package com.my.auth.handler;

import com.my.auth.common.AuthConstants;
import com.my.auth.entity.MyClient;
import com.my.auth.entity.MyUser;
import com.my.auth.security.IClientDetailsService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * @author zhaodongchao
 * @date 2020/3/28 13:58
 **/
@AllArgsConstructor
@Controller
public class LoginController {

    private final IClientDetailsService clientDetailsService ;
    /**
     * 认证页面
     *
     * @param error        表单登录失败处理回调的错误信息
     * @return ModelAndView
     */
    @GetMapping(AuthConstants.LOGIN_URL)
    public ModelAndView require(ModelAndView modelAndView, @RequestParam(required = false) String error) {
        modelAndView.setViewName("login");
        modelAndView.addObject("error", error);
        return modelAndView;
    }

    /**
     * 确认授权页面
     * 这个时候用户已经登录成功
     */
    @GetMapping("/confirm_access")
    public ModelAndView confirm(HttpServletRequest request, HttpSession session, ModelAndView modelAndView) {
        MyClient clientDetails = clientDetailsService.loadClientByClientId("123");
        modelAndView.addObject("scopeList", Arrays.asList("read","write"));
        modelAndView.addObject("app", clientDetails.getAdditionalInformation());
        MyUser user = new MyUser();
        user.setUsername("赵东朝");
        modelAndView.addObject("user", user);
        modelAndView.setViewName("confirm");
        return modelAndView;
    }
}
